//
//  ZCTabBar.swift
//  weibo
//
//  Created by Ning Xie on 16/8/8.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class ZCTabBar: UITabBar {

    var composeButtonClosure :(()->())?
    
    lazy var composeButton: UIButton = {
        
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.addTarget(self, action: #selector(ZCTabBar.composeButtonAction), forControlEvents: .TouchUpInside)
        
        button.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        return button
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(composeButton)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        composeButton.center.x = frame.size.width * 0.5
        composeButton.center.y = frame.size.height * 0.5
        
        let itemWidth = frame.size.width / 5
        var index = 0
        for item in subviews {
            if item.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                item.frame.size.width = itemWidth
                item.frame.origin.x = CGFloat(index) * itemWidth
                index += 1
                if index == 2 {
                    index += 1
                }
            }
        }
    }
    
    @objc private func composeButtonAction() {
        
        composeButtonClosure?()
    }
    

}
